import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';
declare var $: any;
@Component({
	selector: 'sa-contract',
	templateUrl: './contract.component.html',
})
export class ContractComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	modal:any = { //图片弹窗
		picture:'',
		active:'',   //激活的图片
		activeNum:null,  //激活的图片下标
	};
	tableSearch:any = {
	    deviceId: '',
	    carNumber: '',
	    contractNo: '',
		phone: '',
		certificate: '',
		searchKey: '',
		contractType: '',
		companyId: '',
		companyName: ''
	}
	startTime:string='';
	endTime:string='';
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.startTime=time.startTime;
		this.endTime=time.endTime;
	} 
	//[项目车辆]选中回调
    optionSelected(data) {
        this.tableSearch.deviceId = data.id;
        this.tableSearch.carNumber = data.text;
    }
	ngOnInit(){
		this.load();
	}
	//table刷新
    refresh(){
    	this.load();
    }
	//表格搜索
	table_search(){
		this.curPage = 1;
		this.load();
	}
	//初始化加载list数据
	load(){
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
			    deviceId: this.tableSearch.deviceId,
			    companyId: this.tableSearch.companyId,
//			    carNumber: this.tableSearch.carNumber,
			    contractNo: common.stringTrim(this.tableSearch.contractNo),
				phone: common.stringTrim(this.tableSearch.phone),
				certificate: common.stringTrim(this.tableSearch.certificate),
				contractType: this.tableSearch.contractType,
				startTime: this.startTime,
				endTime: this.endTime,
				searchKey: common.stringTrim(this.tableSearch.searchKey)
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("contractV2/electronicContract.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	//合同删除
	delContract(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			this.jsonApiService.http("contractV2/deleteContractById.html",'id='+row.id).subscribe(data=>{
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
	//查看照片
	open_img(row,val){
		val.show();
		if(row.fileUrl){
//			this.modal.active = row.fileUrl;
//			this.modal.activeNum = 0;
			this.modal.picture = row.fileUrl;
		}
	}
//	//图片hover事件
//	switch_img(index){
//		this.modal.activeNum = index;
//		this.modal.active = this.modal.picture[index];
//	}
//	//pre
//	change_img_left(){
//		if(this.modal.activeNum){
//			this.modal.activeNum--;
//			this.modal.active = this.modal.picture[this.modal.activeNum];
//		}
//	}
//	//next
//	change_img_right(e){
//		if(this.modal.activeNum < this.modal.picture.length-1){
//			this.modal.activeNum++;
//			this.modal.active = this.modal.picture[this.modal.activeNum];
//		}
//	}
	//关闭图片弹窗
	imgClose(val){
		val.hide();
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//公司选中回调
	companySelected(data){
		this.tableSearch.companyId = data.companyId;
		this.tableSearch.companyName = data.companyName;
	}
}